﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudentMS
{
    public partial class EditClassForm : Form
    {
        public EditClassForm()
        {
            InitializeComponent();
        }

        private int classId;
        private string oldName = "";
        private int oldGrdeId = 0;
        private Action reLoad = null;

        private void EditClassForm_Load(object sender, EventArgs e)
        {
            InitGradeList();
            InitClassInfo();
        }

        private void InitGradeList()
        {
            string sql = "select GradeId,GradeName from GradeInfo";
            DataTable dataTable = SqlHelper.GetDataTable(sql);
            cboGrades.DataSource = dataTable;
            cboGrades.DisplayMember = "GradeName";
            cboGrades.ValueMember = "GradeId";
        }

        private void InitClassInfo()
        {
            if (this.Tag != null)
            {
                TagObject tagObject = (TagObject)this.Tag;
                classId = tagObject.EditId;
                reLoad = tagObject.ReLoad;
            }
            string sql = "select ClassName,GradeId,Remark from ClassInfo where ClassId=@ClassId";
            SqlParameter paraId = new SqlParameter("@ClassId", classId);
            SqlDataReader reader = SqlHelper.ExecuteReader(sql, paraId);
            if (reader.Read())
            {
                txtClassName.Text = reader["ClassName"].ToString();
                txtRemark.Text = reader["Remark"].ToString();
                int gradeId = (int)reader["GradeId"];
                oldName = txtClassName.Text.ToString();
                oldGrdeId = gradeId;
                cboGrades.SelectedValue = gradeId;
            }
            reader.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string className = txtClassName.Text.Trim();
            int gradeId = (int)cboGrades.SelectedValue;
            string remark = txtRemark.Text.Trim();

            if (string.IsNullOrEmpty(className))
            {
                MessageBox.Show("班级名称不能为空！", "修改班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string sql = "select count(1) from ClassInfo where ClassName=@ClassName and GradeId=@GradeId";
            if (className == oldName && gradeId == oldGrdeId)
            {
                sql += " and ClassId=@ClassId";
            }
            SqlParameter[] paras =
            {
                new SqlParameter("@ClassName", className),
                new SqlParameter("@GradeId", gradeId),
                new SqlParameter("ClassId", classId)
            };
            object obj = SqlHelper.ExecuteScalar(sql, paras);
            if (obj != null && ((int)obj) > 0)
            {
                MessageBox.Show("班级名称已存在！", "修改班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string sqlUpdate = "update ClassInfo set ClassName=@ClassName,Remark=@Remark,GradeId=@GradeId " +
                "where ClassId=@ClassId";
            SqlParameter[] parasEdit =
            {
                    new SqlParameter("@ClassName", className),
                    new SqlParameter("@ClassId", classId),
                    new SqlParameter("@Remark", remark),
                    new SqlParameter("@GradeId", gradeId)
                };
            int count = SqlHelper.ExecuteNonQuery(sqlUpdate, parasEdit);
            if (count > 0)
            {
                MessageBox.Show($"班级：{className}修改成功!", "修改班级提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reLoad.Invoke();
            }
            else
            {
                MessageBox.Show("该班级修改失败，请检查！", "修改班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
